# Input:    /Data/finalData.RData
# Output:   /Paper/Tables/main.tex
#           /Paper/Tables/main_SI.tex
#           /Paper/Figures/sensitivity_dyad.pdf
# Author:   JB


rm(list = ls())
require(tidyverse)
require(ggridges)
require(fixest)
require(marginaleffects)

# source('./helper_functions.R')
load('../data/finalData.RData')


# Rigorous fixed effects
dyadToAnal <- utterance_level %>%
  mutate_at(vars(matches('lag')),function(x) ifelse(is.na(x),0,x)) %>%
  mutate(votepct_rel = ifelse(is.infinite(votepct_rel),1,votepct_rel),
         respondingTo = relevel(factor(respondingTo),ref = 'FEDBERNANKE'))

dims <- colnames(utterance_level %>% select(matches('SENT_'),-matches('_lag|error')) %>% select(-matches('SEVERE|AUTHOR|LIKELY')))

dates <- list(as.Date(c('2006-01-01','2018-01-01')),
              as.Date(c('2008-01-01','2016-01-01')),
              as.Date(c('2013-01-01','2015-01-01')))

# Moderator by first daughter
summary(modFirst <- feols(as.formula(paste0('interruptor ~ fedResp*yellenTime*anyDaughters + nSons + interrupted + log(tot_utterances) + log(nchars_lag)',
                                        # paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                        # ' + ',
                                        # paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                        '| opensecretsID + docID')),
                      utterance_level %>%
                        mutate(yellenTime = ifelse(yellenTime,1,0),
                               anyDaughters = nDaughters > 0) %>%
                        filter(ind > mind,
                               all > 30,
                               date > as.Date(dates[[3]][1]),
                               date < as.Date(dates[[3]][2])),
                      cluster = c('opensecretsID + docID')))

summary(modFirstKids <- feols(as.formula(paste0('interruptor ~ fedResp*yellenTime*firstDaughter + nSons + interrupted + poly(year,3) + log(tot_utterances) + log(nchars_lag) + ',
                                            paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                            ' + ',
                                            paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                            '| opensecretsID + docID')),
                          utterance_level %>%
                            mutate(yellenTime = ifelse(yellenTime,1,0)) %>%
                            filter(ind > mind,
                                   all > 30,
                                   nKids > 0),
                          cluster = c('opensecretsID')))


summary(modFirstM <- feols(as.formula(paste0('interruptor ~ fedResp*yellenTime*firstDaughter + nSons + interrupted + poly(year,3) + log(tot_utterances) + log(nchars_lag) + ',
                                           paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                           ' + ',
                                           paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                           '| opensecretsID + docID')),
                         utterance_level %>%
                           mutate(yellenTime = ifelse(yellenTime,1,0),
                                  anyDaughters = ifelse(nDaughters > 0,1,0)) %>%
                           filter(ind > mind,
                                  all > 30,
                                  gender == 'M'),
                         cluster = c('opensecretsID')))

summary(modFirstF <- feols(as.formula(paste0('interruptor ~ fedResp*yellenTime*firstDaughter + nSons + interrupted + poly(year,3) + log(tot_utterances) + log(nchars_lag) + ',
                                           paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                           ' + ',
                                           paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                           '| opensecretsID + docID')),
                         utterance_level %>%
                           mutate(yellenTime = ifelse(yellenTime,1,0),
                                  anyDaughters = ifelse(nDaughters > 0,1,0)) %>%
                           filter(ind > mind,
                                  all > 30,
                                  gender == 'F'),
                         cluster = c('opensecretsID')))

summary(modAny <- feols(as.formula(paste0('interruptor ~ fedResp*yellenTime*anyDaughters + nSons + interrupted + poly(year,3) + log(tot_utterances) + log(nchars_lag) + ',
                                          paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                          ' + ',
                                          paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                          '| opensecretsID + docID')),
                        utterance_level %>%
                          mutate(yellenTime = ifelse(yellenTime,1,0),
                                 anyDaughters = ifelse(nDaughters > 0,1,0)) %>%
                          filter(ind > mind,all > 30),
                        cluster = c('opensecretsID')))

summary(modAnyKids <- feols(as.formula(paste0('interruptor ~ fedResp*yellenTime*anyDaughters + nSons + interrupted + poly(year,3) + log(tot_utterances) + log(nchars_lag) + ',
                                          paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                          ' + ',
                                          paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                          '| opensecretsID + docID')),
                        utterance_level %>%
                          mutate(yellenTime = ifelse(yellenTime,1,0),
                                 anyDaughters = ifelse(nDaughters > 0,1,0)) %>%
                          filter(ind > mind,all > 30,nKids > 0),
                        cluster = c('opensecretsID')))

summary(modAnyM <- feols(as.formula(paste0('interruptor ~ fedResp*yellenTime*anyDaughters + nSons + interrupted + poly(year,3) + log(tot_utterances) + log(nchars_lag) + ',
                                          paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                          ' + ',
                                          paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                          '| opensecretsID + docID')),
                        utterance_level %>%
                          mutate(yellenTime = ifelse(yellenTime,1,0),
                                 anyDaughters = ifelse(nDaughters > 0,1,0)) %>%
                          filter(ind > mind,
                                 all > 30,
                                 gender == 'M'),
                        cluster = c('opensecretsID')))

summary(modAnyF <- feols(as.formula(paste0('interruptor ~ fedResp*yellenTime*anyDaughters + nSons + interrupted + poly(year,3) + log(tot_utterances) + log(nchars_lag) + ',
                                           paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
                                           ' + ',
                                           paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                           '| opensecretsID + docID')),
                         utterance_level %>%
                           mutate(yellenTime = ifelse(yellenTime,1,0),
                                  anyDaughters = ifelse(nDaughters > 0,1,0)) %>%
                           filter(ind > mind,
                                  all > 30,
                                  gender == 'F'),
                         cluster = c('opensecretsID')))

toplotFirst <- plot_cme(modFirst,variables = 'fedResp',condition = (c('yellenTime','firstDaughter')),draw = F)


toplotFirst <- toplotFirst %>%
  left_join(toplotFirst %>%
              group_by(firstDaughter) %>%
              summarise(diff = diff(estimate),
                        se = sqrt(sum(std.error^2))) %>%
              ungroup() #%>%
              # mutate(pval = 2*pt(diff/se,df = 1000,lower.tail = F),
              #        stars = ifelse(pval < .001,'***',ifelse(pval < .01,'**',
              #                                                ifelse(pval < .05,'*',''))))
            ) %>%
  bind_cols(modFirst$coeftable %>%
              data.frame() %>%
              mutate(vars = row.names(.)) %>%
              filter(grepl('.*:.*:.*',vars)) %>%
              as_tibble() %>%
              select(est = Estimate,pval = Pr...t..,stdErr = Std..Error))



require(ggrepel)
# require(ggpubr)
pFirst <- toplotFirst %>%
  mutate(firstDaughter = ifelse(firstDaughter == 0,'Son','Daughter')) %>%
  ggplot(aes(x = factor(yellenTime),y = estimate,group = firstDaughter,color = firstDaughter,shape = firstDaughter)) + 
  geom_errorbar(aes(ymin = conf.low,ymax = conf.high),width = 0,position = position_dodge(width = 0)) + 
  geom_line(position = position_dodge(width = 0)) + 
  geom_point(position = position_dodge(width = 0)) + 
  theme_ridges() + 
  scale_x_discrete(labels = c('Male Chairs','Yellen'),expand = c(.1,.1,.1,.7)) + 
  geom_hline(yintercept = 0,linetype = 'dashed') + 
  geom_label(data = toplotFirst %>%
               mutate(firstDaughter = ifelse(firstDaughter == 0,'Son','Daughter')) %>%
               filter(yellenTime == 1),#direction = 'y',min.segment.length = 100,
             aes(y = estimate,label = paste0(firstDaughter,': ',round(diff,3),'\n     (',round(se,2),')')),
             hjust = 1.1,size = 3) + 
  geom_segment(data = toplotFirst %>% 
                 filter(yellenTime == 1) %>%
                 select(estimate) %>%
                 distinct() %>%
                 mutate(mn = min(estimate),
                        mx = max(estimate)),
               inherit.aes = F,
               aes(y = mn,yend = mx),x = 2.15,xend = 2.15) + 
  geom_segment(data = toplotFirst %>% 
               filter(yellenTime == 1) %>%
               select(estimate,est,pval) %>%
               distinct() %>%
               summarise(yMid = min(estimate) + abs((diff(estimate)/2))),
             inherit.aes = F,
             x = 2.15,aes(y = yMid,yend = yMid),xend = 2.2) + 
  geom_label(data = toplotFirst %>% 
               filter(yellenTime == 1) %>%
               select(estimate,est,pval,stdErr) %>%
               distinct() %>%
               # group_by(est,pval) %>%
               summarise(yMid = min(estimate) + abs((diff(estimate)/2)),
                         est = abs(mean(est)),
                         pval = mean(pval),
                         se = mean(stdErr)),
             inherit.aes = F,
             x = 2.2,aes(y = yMid,label = paste0('Diff: ',round(est,3),'\n       (',round(se,2),')')),
             hjust = 0,size = 3) + 
  # annotate(geom = 'label',aes(label = paste0(round(est,3),'\n(',round(se,3),')'))) + 
  scale_color_manual(guide = 'none',name = 'Party',values = c('orange','brown')) + 
  scale_shape_discrete(guide = 'none',name = 'Party') + 
  labs(x = NULL,y = NULL,
       subtitle = "First Child's Gender")
pFirst





toplotAny <- plot_cme(modAny,variables = 'fedResp',condition = (c('yellenTime','anyDaughters')),draw = F)

toplotAny <- toplotAny %>%
  left_join(toplotAny %>%
              group_by(anyDaughters) %>%
              summarise(diff = diff(estimate),
                        se = sqrt(sum(std.error^2))) %>%
              ungroup() #%>%
            # mutate(pval = 2*pt(diff/se,df = 1000,lower.tail = F),
            #        stars = ifelse(pval < .001,'***',ifelse(pval < .01,'**',
            #                                                ifelse(pval < .05,'*',''))))
  ) %>%
  bind_cols(modAny$coeftable %>%
              data.frame() %>%
              mutate(vars = row.names(.)) %>%
              filter(grepl('.*:.*:.*',vars)) %>%
              as_tibble() %>%
              select(est = Estimate,pval = Pr...t..,stdErr = Std..Error))



require(ggrepel)
# require(ggpubr)
pAny <- toplotAny %>%
  mutate(anyDaughters = ifelse(anyDaughters == 0,'None','1+')) %>%
  ggplot(aes(x = factor(yellenTime),y = estimate,group = anyDaughters,color = anyDaughters,shape = anyDaughters)) + 
  geom_errorbar(aes(ymin = conf.low,ymax = conf.high),width = 0,position = position_dodge(width = 0)) + 
  geom_line(position = position_dodge(width = 0)) + 
  geom_point(position = position_dodge(width = 0)) + 
  theme_ridges() + 
  scale_x_discrete(labels = c('Male Chairs','Yellen'),expand = c(.1,.1,.1,.7)) + 
  geom_hline(yintercept = 0,linetype = 'dashed') + 
  geom_label(data = toplotAny %>%
               mutate(anyDaughters = ifelse(anyDaughters == 0,'None','1+')) %>%
               filter(yellenTime == 1),#direction = 'y',min.segment.length = 100,
             aes(y = estimate,label = paste0(anyDaughters,': ',round(diff,3),'\n     (',round(se,2),')')),
             hjust = 1.1,
             direction = 'y',min.segment.length = Inf,
             size = 3) + 
  geom_segment(data = toplotAny %>% 
                 filter(yellenTime == 1) %>%
                 select(estimate) %>%
                 distinct() %>%
                 mutate(mn = min(estimate),
                        mx = max(estimate)),
               inherit.aes = F,
               aes(y = mn,yend = mx),x = 2.15,xend = 2.15) + 
  geom_segment(data = toplotAny %>% 
                 filter(yellenTime == 1) %>%
                 select(estimate,est,pval) %>%
                 distinct() %>%
                 summarise(yMid = min(estimate) + abs((diff(estimate)/2))),
               inherit.aes = F,
               x = 2.15,aes(y = yMid,yend = yMid),xend = 2.2) + 
  geom_label(data = toplotAny %>% 
               filter(yellenTime == 1) %>%
               select(estimate,est,pval,stdErr) %>%
               distinct() %>%
               # group_by(est,pval) %>%
               summarise(yMid = min(estimate) + abs((diff(estimate)/2)),
                         est = abs(mean(est)),
                         pval = mean(pval),
                         se = mean(stdErr)),
             inherit.aes = F,
             x = 2.2,aes(y = yMid,label = paste0('Diff: ',round(est,3),'\n       (',round(se,2),')')),
             hjust = 0,size = 3) + 
  # annotate(geom = 'label',aes(label = paste0(round(est,3),'\n(',round(se,3),')'))) + 
  scale_color_manual(guide = 'none',name = 'Party',values = c('orange','brown')) + 
  scale_shape_discrete(guide = 'none',name = 'Party') + 
  labs(x = NULL,y = NULL,
       subtitle = 'Any Daughters')
pAny


toplotAnyM <- plot_cme(modAnyM,variables = 'fedResp',condition = (c('yellenTime','anyDaughters')),draw = F)

toplotAnyM <- toplotAnyM %>%
  left_join(toplotAnyM %>%
              group_by(anyDaughters) %>%
              summarise(diff = diff(estimate),
                        se = sqrt(sum(std.error^2))) %>%
              ungroup() #%>%
            # mutate(pval = 2*pt(diff/se,df = 1000,lower.tail = F),
            #        stars = ifelse(pval < .001,'***',ifelse(pval < .01,'**',
            #                                                ifelse(pval < .05,'*',''))))
  ) %>%
  bind_cols(modAnyM$coeftable %>%
              data.frame() %>%
              mutate(vars = row.names(.)) %>%
              filter(grepl('.*:.*:.*',vars)) %>%
              as_tibble() %>%
              select(est = Estimate,pval = Pr...t..,stdErr = Std..Error))



require(ggrepel)
# require(ggpubr)
pAnyM <- toplotAnyM %>%
  mutate(anyDaughters = ifelse(anyDaughters == 0,'None','1+')) %>%
  ggplot(aes(x = factor(yellenTime),y = estimate,group = anyDaughters,color = anyDaughters,shape = anyDaughters)) + 
  geom_errorbar(aes(ymin = conf.low,ymax = conf.high),width = 0,position = position_dodge(width = 0)) + 
  geom_line(position = position_dodge(width = 0)) + 
  geom_point(position = position_dodge(width = 0)) + 
  theme_ridges() + 
  scale_x_discrete(labels = c('Male Chairs','Yellen'),expand = c(.1,.1,.1,.7)) + 
  geom_hline(yintercept = 0,linetype = 'dashed') + 
  geom_label(data = toplotAnyM %>%
               mutate(anyDaughters = ifelse(anyDaughters == 0,'None','1+')) %>%
               filter(yellenTime == 1),#direction = 'y',min.segment.length = 100,
             aes(y = estimate,label = paste0(anyDaughters,': ',round(diff,3),'\n     (',round(se,2),')')),
             hjust = 1.1,size = 3) + 
  geom_segment(data = toplotAnyM %>% 
                 filter(yellenTime == 1) %>%
                 select(estimate) %>%
                 distinct() %>%
                 mutate(mn = min(estimate),
                        mx = max(estimate)),
               inherit.aes = F,
               aes(y = mn,yend = mx),x = 2.15,xend = 2.15) + 
  geom_segment(data = toplotAnyM %>% 
                 filter(yellenTime == 1) %>%
                 select(estimate,est,pval) %>%
                 distinct() %>%
                 summarise(yMid = min(estimate) + abs((diff(estimate)/2))),
               inherit.aes = F,
               x = 2.15,aes(y = yMid,yend = yMid),xend = 2.2) + 
  geom_label(data = toplotAnyM %>% 
               filter(yellenTime == 1) %>%
               select(estimate,est,pval,stdErr) %>%
               distinct() %>%
               # group_by(est,pval) %>%
               summarise(yMid = min(estimate) + abs((diff(estimate)/2)),
                         est = abs(mean(est)),
                         pval = mean(pval),
                         se = mean(stdErr)),
             inherit.aes = F,
             x = 2.2,aes(y = yMid,label = paste0('Diff: ',round(est,3),'\n       (',round(se,2),')')),
             hjust = 0,size = 3) + 
  # annotate(geom = 'label',aes(label = paste0(round(est,3),'\n(',round(se,3),')'))) + 
  scale_color_manual(guide = 'none',name = 'Party',values = c('orange','brown')) + 
  scale_shape_discrete(guide = 'none',name = 'Party') + 
  labs(x = NULL,y = NULL,
       subtitle = 'Any Daughters: Men')
pAnyM



toplotAnyF <- plot_cme(modAnyF,variables = 'fedResp',condition = (c('yellenTime','anyDaughters')),draw = F)

toplotAnyF <- toplotAnyF %>%
  left_join(toplotAnyF %>%
              group_by(anyDaughters) %>%
              summarise(diff = diff(estimate),
                        se = sqrt(sum(std.error^2))) %>%
              ungroup() #%>%
            # mutate(pval = 2*pt(diff/se,df = 1000,lower.tail = F),
            #        stars = ifelse(pval < .001,'***',ifelse(pval < .01,'**',
            #                                                ifelse(pval < .05,'*',''))))
  ) %>%
  bind_cols(modAnyF$coeftable %>%
              data.frame() %>%
              mutate(vars = row.names(.)) %>%
              filter(grepl('.*:.*:.*',vars)) %>%
              as_tibble() %>%
              select(est = Estimate,pval = Pr...t..,stdErr = Std..Error))



require(ggrepel)
# require(ggpubr)
pAnyF <- toplotAnyF %>%
  mutate(anyDaughters = ifelse(anyDaughters == 0,'None','1+')) %>%
  ggplot(aes(x = factor(yellenTime),y = estimate,group = anyDaughters,color = anyDaughters,shape = anyDaughters)) + 
  geom_errorbar(aes(ymin = conf.low,ymax = conf.high),width = 0,position = position_dodge(width = 0)) + 
  geom_line(position = position_dodge(width = 0)) + 
  geom_point(position = position_dodge(width = 0)) + 
  theme_ridges() + 
  scale_x_discrete(labels = c('Male Chairs','Yellen'),expand = c(.1,.1,.1,.7)) + 
  geom_hline(yintercept = 0,linetype = 'dashed') + 
  geom_label_repel(data = toplotAnyF %>%
               mutate(anyDaughters = ifelse(anyDaughters == 0,'None','1+')) %>%
               filter(yellenTime == 1),#direction = 'y',min.segment.length = 100,
             aes(y = estimate,label = paste0(anyDaughters,': ',round(diff,3),'\n     (',round(se,2),')')),
             nudge_x = -.1,hjust = 1,direction = 'y',size = 3,min.segment.length = Inf) + 
  geom_segment(data = toplotAnyF %>% 
                 filter(yellenTime == 1) %>%
                 select(estimate) %>%
                 distinct() %>%
                 mutate(mn = min(estimate),
                        mx = max(estimate)),
               inherit.aes = F,
               aes(y = mn,yend = mx),x = 2.15,xend = 2.15) + 
  geom_segment(data = toplotAnyF %>% 
                 filter(yellenTime == 1) %>%
                 select(estimate,est,pval) %>%
                 distinct() %>%
                 summarise(yMid = min(estimate) + abs((diff(estimate)/2))),
               inherit.aes = F,
               x = 2.15,aes(y = yMid,yend = yMid),xend = 2.2) + 
  geom_label(data = toplotAnyF %>% 
               filter(yellenTime == 1) %>%
               select(estimate,est,pval,stdErr) %>%
               distinct() %>%
               # group_by(est,pval) %>%
               summarise(yMid = min(estimate) + abs((diff(estimate)/2)),
                         est = abs(mean(est)),
                         pval = mean(pval),
                         se = mean(stdErr)),
             inherit.aes = F,
             x = 2.2,aes(y = yMid,label = paste0('Diff: ',round(est,3),'\n       (',round(se,2),')')),
             hjust = 0,size = 3) + 
  # annotate(geom = 'label',aes(label = paste0(round(est,3),'\n(',round(se,3),')'))) + 
  scale_color_manual(guide = 'none',name = 'Party',values = c('orange','brown')) + 
  scale_shape_discrete(guide = 'none',name = 'Party') + 
  labs(x = NULL,y = NULL,
       subtitle = 'Any Daughters: Women')
pAnyF



require(patchwork)

pdf('../output/figures/anyDaughters_mfx.pdf',width = 8,height = 5)
pAny + (pAnyM / pAnyF)
dev.off()